# README

## Overview
This script performs Generalized Procrustes Analysis (GPA), Principal Component Analysis (PCA), and other morphometric analyses on 3D semilandmark data. The input data consists of CSV files with semilandmark coordinates for multiple specimens.

## Prerequisites
Ensure you have R installed on your system (≥ 4.0). The following R packages are required:

1. **geomorph** (≥ 4.0): For GPA and morphometric analyses.
   - Install using: `install.packages("geomorph")`
2. **ade4** (≥ 1.7-20): For PCA and visualization functions.
   - Install using: `install.packages("ade4")`
3. **stats** and **utils**: These are base R packages and come pre-installed with R.

## Files
- **Source Data_1.csv** to **Source Data_5.csv**: Input file containing semilandmark coordinates.

## Usage
1. Place the CSV files in the working directory of your R session.
2. Update the `Label` variable with your specimen labels.
3. Run the script in R to:
   - Load and preprocess the data.
   - Perform GPA to align 3D points.
   - Identify outliers and extract aligned coordinates.
   - Conduct PCA and visualize the results.

## Script Breakdown
### Step 1: Load Data
Loads semilandmark data from two CSV files, converting them into a matrix for analysis.

### Step 2: Data Reshaping
Reshapes the data into a 3D array with dimensions: number of landmarks, X/Y/Z coordinates, and individuals.

### Step 3: Define Labels
Assigns labels to specimens for identification in analyses and visualizations.

### Step 4: GPA Analysis
Aligns 3D semilandmarks using Generalized Procrustes Analysis (GPA). Detects outliers and extracts aligned coordinates.

### Step 5: PCA and bgPCA
Performs PCA on aligned data to reduce dimensionality and interpret shape variation.

## Visualization
The script includes optional visualization functions to:
- Plot specimens (`plotAllSpecimens`)
- Identify outliers (`plotOutliers`)
- Visualize PCA results (`s.label`, `s.class`, `s.traject`, `s.corcircle`)

## Output
The script outputs:
- Aligned coordinates (`aligned_coords`)
- PCA results (`PCA`, `RESPCA`)
- Visualization plots (if uncommented)

## Notes
- Ensure your input files follow the expected format (see example files for structure).
- For custom specimen labels, update the `Label` variable in the script.

## Contact
For questions or issues, contact the script author or consult the documentation of the geomorph and ade4 packages.